//
//  EditViewController.m
//  09-UITableView省市区
//
//  Created by LL.F on 16/7/26.
//  Copyright © 2016年 LL.F. All rights reserved.
//

#import "EditViewController.h"
#import "DetailViewController.h"
#import "AddViewController.h"

#define kScreenbounds [UIScreen mainScreen].bounds
#define kScreenWidth [UIScreen mainScreen].bounds.size.width
#define kScreenHeight  [UIScreen mainScreen].bounds.size.height
@interface EditViewController ()<UITableViewDataSource, UITableViewDelegate, PassValueDelegate>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *dataSourceArray;
// 选中下标的数组
@property (nonatomic, strong) NSMutableArray<NSIndexPath *> *selectIndexArray;
// 选中名字的数组
@property (nonatomic, strong) NSMutableArray<NSString *> *selectedNameArray;

@end

@implementation EditViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationItem.title = @"通讯录编辑";
    self.view.backgroundColor = [UIColor colorWithRed:1.000 green:0.612 blue:0.396 alpha:1.000];
    
    
    /*
     1.
     根据给定的plist文件(讲师教给学员怎么使用，读取数据）, 使用tableView写一个通讯录, 要求:
     <1>第一个页面每个cell上显示学员姓名
     <2>点击某个学员, 推出到详细信息页面
     <3>详细信息页面使用label显示学员的详细信息
     NSString *path = [[NSBundle mainBundle] pathForResource:@"DLS160710" ofType:@"plist"];
     NSArray *array = [NSArray arrayWithContentsOfFile:path];
     2.
     在导航栏上增加一个按钮可以push一个添加学员页面
     添加学员页面中使用textField让用户输入信息, 点击保存按钮将学员信息显示到第一页tableView的最后面
     
     提示:
     tableView有一个方法 - (void)reloadData; 可以重载所有的dataSource协议方法.因此可以把数据添加到数组中, 然后reloadData即可.
     使用协议传值
     */
    
    self.navigationItem.title = @"通讯录";
   
    self.tableView = [[UITableView alloc] initWithFrame: CGRectMake(0, 0, kScreenWidth, kScreenHeight - 49 ) style:UITableViewStylePlain];
    [self.view addSubview:_tableView];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    
    //处理数据
    NSString *path = [[NSBundle mainBundle] pathForResource:@"DLS160710" ofType:@"plist"];
    NSArray *array = [NSArray arrayWithContentsOfFile:path];
    self.dataSourceArray = [NSMutableArray arrayWithArray:array];
    
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(didClickedAddButton:)];
    self.navigationController.navigationBar.tintColor = [UIColor cyanColor];
    
    // <2-1>添加编辑按钮
    self.navigationItem.leftBarButtonItem = self.editButtonItem;
    
    
}
// 右侧"添加"按钮
- (void)didClickedAddButton:(UIBarButtonItem *)button
{
    
    AddViewController *addVC = [[AddViewController alloc] init];
    addVC.delegate = self;
    [self.navigationController pushViewController:addVC animated:YES];
    
}
// 实现协议方法
- (void)PassValue:(NSDictionary *)dictionary
{
    // 名字不为空才添加
    if (![[dictionary valueForKey:@"name"] isEqualToString:@""]) {
        [self.dataSourceArray addObject:dictionary];
        [self.tableView reloadData];
    }
    
    
}
#pragma mark = UITableView Delegate
// 返回tableView编辑样式
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{

    return UITableViewCellEditingStyleDelete;
}
#pragma mark - UITableView DataSource
// <1-1>
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    return _dataSourceArray.count;
    
}
// <1-2>
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    cell.textLabel.text = [_dataSourceArray[indexPath.row] valueForKey:@"name"];
    return cell;
    
}
// <1-3>
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    DetailViewController *detailVC = [[DetailViewController alloc] init];
    // 传值
    // 方法一:
    //NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    //dic = _dataSourceArray[indexPath.row];
    //detailVC.contactDic = dic;
    
    //方法二:
    detailVC.contactDic = [_dataSourceArray objectAtIndex:indexPath.row];
    
    [self.navigationController pushViewController:detailVC animated:YES];
    
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        NSLog(@"删除");
        [_dataSourceArray removeObjectAtIndex:indexPath.row];
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft];
    }
}

// <2-3> 控制tableView能进行编辑
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    return YES;
}

// <2-2> 设置可编辑状态
#pragma mark -- viewControler 类目方法
- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{

    [super setEditing:editing animated:animated];
    [self.tableView setEditing:editing animated:animated];
    if (editing) {
        NSLog(@"编辑");
    } else {
    
        
        [_dataSourceArray removeObjectsInArray:_selectedNameArray];
        // 删除cell
        [_tableView deleteRowsAtIndexPaths:_selectIndexArray withRowAnimation:UITableViewRowAnimationLeft];
        // 删除一次后要进行清空
        [_selectedNameArray removeAllObjects];
        [_selectIndexArray removeAllObjects];
    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
